package ca.camosun.chatbox.network;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import ca.camosun.chatbox.profile.Profile;

public class Room<T> implements Iterable<T> {
	public Set<T> people;
	
	public Room() {
		people = new TreeSet<T>();
	}
	
	public void changeRoomContents(Collection<T> newPeople) {
		people = new TreeSet<T>();
		people.addAll(newPeople);
	}
	
	public int size() {
		return people.size();
	}
	
	public boolean contains(T who) {
		return people.contains(who);
	}
	
	public boolean containsAll(Collection<T> who) {
		return people.containsAll(who);
	}
	
	public Set<T> getDepartures(Room<T> newRoom) {
		Set<T> ourPeeps = new TreeSet<T>(people);
		Set<T> theirPeeps = new TreeSet<T>(newRoom.people);
		
		Set<T> peopleInCommon = new TreeSet<T>(ourPeeps);
		peopleInCommon.retainAll(theirPeeps);		
		
		ourPeeps.removeAll(peopleInCommon);
		ourPeeps.removeAll(theirPeeps);
		
		return ourPeeps;
	}
	
	public Set<T> getArrivals(Room<T> newRoom) {
		Set<T> ourPeeps = new TreeSet<T>(people);
		Set<T> theirPeeps = new TreeSet<T>(newRoom.people);
		
		Set<T> peopleInCommon = new TreeSet<T>(ourPeeps);
		peopleInCommon.retainAll(theirPeeps);

		theirPeeps.removeAll(peopleInCommon);
		theirPeeps.removeAll(ourPeeps);
		
		return theirPeeps;
	}

	@Override
	public Iterator<T> iterator() {
		return people.iterator();
	}
}
